package cn.edu.ctbu.BoostrapTable.service;

import cn.edu.ctbu.BoostrapTable.domain.Student;
import cn.edu.ctbu.BoostrapTable.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentService {
    @Autowired
    private StudentRepository studentRepository;

    /**
     * 分页用查找全部数据
     */
    public Page<Student> getAll(Pageable pageable){
        return studentRepository.findAll(pageable);
    }
    /**
     * 查找所有数据
     */
    public List<Student> findAll(){
        return studentRepository.findAll();
    }

    /**
     * 根据id查找
     */
    public Student findById(Integer id){
        return studentRepository.findById(id).orElse(null);
    }
    /**
     * 根据name查找
     */
    public List<Student> findByName(String name){
        return studentRepository.findByName(name);
    }

    /**
     * 根据id删除数据
     */
    public  void delete(Integer id){
        studentRepository.deleteById(id);
    }

    /**
     * 增加数据
     */
    public Student insert(Student student){
        Student student1=studentRepository.save(student);
        return student1;
    }

    /**
     * 修改数据
     */
    public  void update(Student student){
        studentRepository.save(student);
    }
}
